﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ClassLibrary;

namespace ClassLibrary
{
    public class GatewayPatient : DataAccess<Patient> //TESTING!!!!
    {

        public override Patient Create(Patient objectToCreate)
        {
            CreateCon();

            System.Data.IDbCommand command = con.CreateCommand();
            
            command.CommandText = "INSERT INTO PATIENT "
                                    + "(CPR, Name, Adresse, Zip, City, Phonenumber, Email) VALUES " +
                                     "('" + objectToCreate.Cpr + "', '" + objectToCreate.Name + "', '" + objectToCreate.Address + "', " + objectToCreate.Zip + ", '" + objectToCreate.City + "', " + objectToCreate.Phonenumber + ",  '" + objectToCreate.Email + "')";

            int antalIndsat = command.ExecuteNonQuery();
             if (antalIndsat == 1)
            {
                command.CommandText = "SELECT @@identity AS lastinsertedkey";
                string slastkey = command.ExecuteScalar().ToString();
                int lastkey = int.Parse(slastkey);
                //7
                objectToCreate.Id = lastkey;
                EndCon();
                return objectToCreate;
            }
            EndCon();
            return null;
        }

        public override Patient Update(Patient objectToUpdate)
        {
            CreateCon();

            System.Data.IDbCommand command = con.CreateCommand();
            command.CommandText = "UPDATE PATIENT SET " + "PatientID=" + objectToUpdate.Id + " " + ", CPR='" + objectToUpdate.Cpr + "', Name='" + objectToUpdate.Name + "',Adresse=" + objectToUpdate.Address + ", Zip=" + objectToUpdate.Zip + ", City='" + objectToUpdate.City + "', Phonenumber=" + objectToUpdate.Phonenumber + "', Email=" + objectToUpdate.Email + " " + "WHERE PatientID=" + objectToUpdate.Id;
            int antalOpdateret = command.ExecuteNonQuery();
            EndCon();
            return objectToUpdate;

        }

        public override bool Delete(Patient objectToDelete)
        {
            CreateCon();

            System.Data.IDbCommand command = con.CreateCommand();
            command.CommandText = "UPDATE PATIENT SET " + "Deleted=" + 1 + " WHERE PatientID=" + objectToDelete.Id;
            int antalOpdateret = command.ExecuteNonQuery();
            EndCon();
            return antalOpdateret == 1;
        }

        public override Patient Load(int idOfObjectToLoad)
        {
            CreateCon();

            System.Data.IDbCommand command = con.CreateCommand();
            command.CommandText = "SELECT * FROM Patient WHERE (PatientID =" + idOfObjectToLoad + "  )";
            System.Data.IDataReader reader = command.ExecuteReader();

            reader.Read();

                int PatientID = int.Parse(reader["PatientID"].ToString());
                string CPR = reader["CPR"].ToString();
                string Name = reader["Name"].ToString();
                string Adresse = reader["Adresse"].ToString();
                int Zip = int.Parse(reader["Zip"].ToString());
                string City = reader["City"].ToString();
                int Phonenumber = int.Parse(reader["Phonenumber"].ToString());
                string Email = reader["Email"].ToString();


                Patient nyPatient = new Patient(CPR, Name, Adresse, Zip, City, Phonenumber, Email);
                nyPatient.Id = PatientID;

            EndCon();
            return nyPatient;
        }

        public override List<Patient> LoadAll()
        {
            CreateCon();

            List<Patient> nyPatientListe = new List<Patient>();

            System.Data.IDbCommand command = con.CreateCommand();
            command.CommandText = "SELECT * FROM Patient WHERE (Deleted = 'false') OR (Deleted IS NULL)";
            System.Data.IDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                int PatientID = int.Parse(reader["PatientID"].ToString());
                string CPR = reader["CPR"].ToString();
                string Name = reader["Name"].ToString();
                string Adresse = reader["Adresse"].ToString();
                int Zip = int.Parse(reader["Zip"].ToString());
                string City = reader["City"].ToString();
                int Phonenumber = int.Parse(reader["Phonenumber"].ToString());
                string Email = reader["Email"].ToString();

                Patient NyPatient = new Patient(CPR, Name, Adresse, Zip, City, Phonenumber, Email);
                NyPatient.Id = PatientID;

                nyPatientListe.Add(NyPatient);
                
                
            }

            EndCon();
            return nyPatientListe;
        }

            /* public string Create(string cpr, string name, string adresse,int zip,string city,int phonenumber, string email)
             {
                 CreateCon(); 

                 Patient NewPatient = null;

                 System.Data.IDbCommand command = con.CreateCommand();
                 command.CommandText = "INSERT INTO PATIENT "
                                         + "(CPR, Name, Adresse, Zip, City, Phonenumer, Email,) VALUES " +
                                          "(" + cpr + ", '" + name + "', '" + adresse + "', " + zip + ", '" + city + "', " + phonenumber + ", " + email + ")";



                 int antalIndsat = command.ExecuteNonQuery();
                 if (antalIndsat == 1)
                 {
                     command.CommandText = "SELECT @@identity AS lastinsertedkey";
                     string slastkey = command.ExecuteScalar().ToString();
                     int lastkey = int.Parse(slastkey);
                     command.CommandText = "SELECT * FROM PATIENT "
                                            + " WHERE (CPR=" + lastkey + ")";
                     System.Data.IDataReader reader = command.ExecuteReader();
                     if (reader.Read())
                     {
                         string NewCPR = reader["CPR"].ToString();
                         string NewName = (reader["Name"].ToString());
                         string NewAdresse = reader["Adresse"].ToString();
                         int NewZip = int.Parse(reader["Zip"].ToString());
                         string NewCity = reader["City"].ToString();
                         int NewPhonenumber = int.Parse(reader["Phonenumber"].ToString());
                         string NewEmail = reader["Email"].ToString();
                    


                         NewPatient = new Patient(cpr, name, adresse, zip, city, phonenumber, email);

                         return NewPatient;
                    
                     }
                 }
             }

             public string Save()
             {
                 throw new NotImplementedException();
             }

             public string Load()
             {
                 throw new NotImplementedException();
             }

             public string Update()
             {
                 throw new NotImplementedException();
             }

             public string Delete()
             {
                 throw new NotImplementedException();
             }*/


        //internal GatewayPatient Create(string cpr, string name, string adresse, int zip, string city, int phonenumber, string email)
        //{
        //    throw new NotImplementedException();
        //}
    }
    }
